package com.atguigu.springcloud.filter;

import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 2 * @Author: madonghui
 * 3 * @Date: 2020/11/3 16:16
 * 4
 */
@Component
@Slf4j
public class MyLogGatewayfilter implements GlobalFilter, Ordered {
    /**
     *
     * @param exchange 相当于applicationcontext
     * @param chain
     * @return  Mono 相当于modelandview
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("羁绊是什么意思，欢迎来到过滤器");
        String name = exchange.getRequest().getQueryParams().getFirst("name");
        if (StringUtil.isNullOrEmpty(name)){
            log.info("非法用户，┭┮﹏┭┮");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    /**
     * 作用就是获取优先级，过滤器数字越小优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
